package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.makerspace.dao.MessageDAO;
import com.example.makerspace.dao.UserDAO;
import com.example.makerspace.domain.MessageDO;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.MessageDTO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.dto.Result;
import com.example.makerspace.service.MessageService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpI implements MessageService {

    @Resource
    private MessageDAO messageDAO;

    @Resource
    private UserDAO userDAO;
    @Override
    public Result addMessage(MessageDTO messageDTO) {
        MessageDO messageDO = new MessageDO();
        BeanUtils.copyProperties(messageDTO, messageDO);
        if (messageDTO.getType() == 0&&messageDTO.getUsername().equals("admin")) {
            boolean insert = messageDAO.insert(messageDO);
            List<MessageDO> messageDOS = messageDAO.select();
            if (!CollectionUtils.isEmpty(messageDOS)) {
                MessageDO messageDO1 = messageDOS.get(0);
                //系统消息
                List<UserDO> userDOS = userDAO.selectAll();
                for (UserDO user : userDOS) {
                    List<String> unreadSystemMessage = user.getUnreadSystemMessage();
                    if (unreadSystemMessage == null) {
                        ArrayList<String> unreadSystemMessages = new ArrayList<>();
                        unreadSystemMessages.add(String.valueOf(messageDO1.getId()));
                        user.setUnreadSystemMessage(unreadSystemMessages);
                        userDAO.updateById(user);
                    }else {
                        unreadSystemMessage.add(String.valueOf(messageDO1.getId()));
                        user.setUnreadSystemMessage(unreadSystemMessage);
                        userDAO.updateById(user);
                    }
                }
            }
            return  Result.success("新增消息成功");
        }else {
            boolean insert = messageDAO.insert(messageDO);
            return  Result.success("新增消息成功");
        }

    }

    @Override
    public Result updateMessage(MessageDTO messageDTO) {
        MessageDO messageDO = new MessageDO();
        BeanUtils.copyProperties(messageDTO,messageDO);
        messageDAO.updateById(messageDO);
        return  Result.success("修改消息成功");
    }

    @Override
    public Result deleteMessage(Integer id) {
        MessageDO messageDO = messageDAO.selectById(id);
        if(messageDO.getType()==0){
            //如果为系统消息
            List<UserDO> userDOS = userDAO.selectAll();
            for(int i=0;i<userDOS.size();i++){
                String  str=id.toString();
                userDOS.remove(str);
            }
        }
        messageDAO.deleteById(id);
        return  Result.success("删除消息成功");
    }

    @Override
    public Result selectMessage(PageRequest pageRequest) {
        IPage<MessageDO> messageDOIPage;
        Pagination pagination = pageRequest.getPagination();
        Map<String, Object> params = pageRequest.getParams();
        String username = MapUtils.getString(params, "username");
        String type = MapUtils.getString(params, "type");
        if (username.equals("")) {
            if (type.equals("")) {
                messageDOIPage = messageDAO.selectByPage(pagination);
                return Result.success(messageDOIPage);
            } else {
                messageDOIPage = messageDAO.selectByJustType(pagination, type);
                return Result.success(messageDOIPage);
            }
        } else {
            if (type.equals("")) {
                messageDOIPage = messageDAO.selectByLiskUsername(pagination, username);
                return Result.success(messageDOIPage);
            } else {
                messageDOIPage = messageDAO.selectByLiskUsernameAndType(pagination, username, type);
                return Result.success(messageDOIPage);
            }
        }
    }

    @Override
    public Result selectMessageByUserName(PageRequest pageRequest) {
        Pagination pagination = pageRequest.getPagination();
        Map<String, Object> params = pageRequest.getParams();
        String username = MapUtils.getString(params, "username");
        String type = MapUtils.getString(params, "type");
        if(type.equals("")){
            IPage<MessageDO> messageDOIPage = messageDAO.selectByUserName(pagination,username);
            return Result.success(messageDOIPage);
        }else {
            IPage<MessageDO> messageDOIPage = messageDAO.selectByType(pagination,username,type);
            return Result.success(messageDOIPage);
        }
    }
    @Override
    public Result selectMessageDetail(Integer id,String username) {
        MessageDO messageDO = messageDAO.selectById(id);
        if(messageDO.getType()==0){
            UserDO userDO = userDAO.selectByUsername(username);
            List<String> unreadSystemMessage = userDO.getUnreadSystemMessage();
            if(!CollectionUtils.isEmpty(unreadSystemMessage)){
                String str = id.toString();
                unreadSystemMessage.remove(str);
            }
            userDO.setUnreadSystemMessage(unreadSystemMessage);
            userDAO.updateByUserName(userDO);
        }
        messageDO.setIsRead(1);
        messageDAO.updateById(messageDO);
        return Result.success(messageDO);
    }

    @Override
    public Result showAllUnRead(String username) {
        int sum = messageDAO.selectAllNoRead(username);
        return Result.success(sum);
    }

    @Override
    public Result showAllSystemUnRead(String username) {
        UserDO userDO = userDAO.selectByUsername(username);
        if(userDO.getUnreadSystemMessage()!=null){
            return Result.success(userDO.getUnreadSystemMessage().size());
        }else {
            return Result.success(0);
        }

    }

    @Override
    public Result showAllNoticeUnRead(String username) {
        int sum = messageDAO.showAllNoticeUnRead(username);
        return Result.success(sum);
    }
}
